<?php
/*
*发票接口类
*/
class kehuInvoiceAppClass extends appBase{
	/*查询条件*/
	public function search(){
		//权限
		if($this->power("kehuInvoice","seeAll")){
			$stid = "是";
		}else{
			$stid = "否";
		}
		//数据
		$json['search'] = array(
			"open" => array("是","否"),//是否开票
			"stid" => $stid,//查询所有开票记录
			"company" => array("雨木","云竹"),//销售方
			"type" => array("专票","普票"),//发票类型
		);
		//返回
		$json['warn'] = "success";
		return $json;
	}
	/*列表页*/
	public function home(){
		//全等查询数组
		$equal = array(
			"stid" => $this->post['stid'],//所属员工
			"company" => $this->post['company'],//销售方
			"type" => $this->post['type'],//发票类型
			"open" => $this->post['open'],//是否开票
		);
		//大于等于查询数组
		$big = array(
			"openDate" => $this->post['startDay'],//开票日期-开始日期
		);
		//小于等于查询数组
		$small = array(
			"openDate" => $this->post['endDay'],//开票日期-结束日期
		);
		$and = "";
		//购买方
		if(!empty($this->post['khName'])){
			$and .= " and khid in ( select khid from kehu where companyName like '%".$this->post['khName']."%' ) ";
		}
		//如果没有查看所有员工发票的权限
		if(!$this->power("kehuInvoice","seeAll")){
			$and .= " and stid = '".$this->control['stid']."' ";
		}
		//获取查询结果
		$where = search(array("=" => $equal,">=" => $big,"<=" => $small)).$and;
		$page = $this->page(array("table" => "kehuInvoice","where" => $where,"order" => "openDate desc,time desc"));
		//数据准备
		$json['invoice'] = array();
		foreach($page['array'] as $array){
			$staff = query("staff","stid = '$array[stid]'");
			$kehu = query("kehu","khid = '$array[khid]'");
			$json['invoice'][] = array(
				"id" => $array['id'],//发票ID
				"stffName" => $staff['name'],//开票人
				"type" => $array['type'],//发票类型
				"money" => $array['money'],//价税合计
				"invoiceNum" => $array['invoiceNum'],//发票编号
				"openDate" => $array['openDate'],//开票日期
				"open" => $array['open'],//开票状态
				"companyName" => $kehu['companyName'],//购买方
			);
		}
		//分页信息
		$json['page'] = $page['para'];
		//提示
		$json['warn'] = "success";
		//返回
		return $json;
	}
	/*开票详情*/
	public function detail(){
		$kehuInvoice = query("kehuInvoice","id = '".$this->post['id']."'");
		if(empty($this->post['id'])){
			$json['warn'] = "开票ID为空";
		}elseif(empty($kehuInvoice['id'])){
			$json['warn'] = "未找到此开票记录";
		}elseif(!$this->power("kehuInvoice","seeAll") and $kehuInvoice['stid'] != $this->control['stid']){
			$json['warn'] = "这不是您申请的发票，也没有查看所有发票的权限";
		}else{
			$staff = query("staff","stid = '$kehuInvoice[stid]'");
			$kehu = query("kehu","khid = '$kehuInvoice[khid]'");
			//开票金额
			$moneyInvoice = round($kehuInvoice['money']/1.03,2);
			//纳税金额
			$moneyTax = $kehuInvoice['money'] - $moneyInvoice;
			//返回
			$json['invoice'] = array(
				"company" => $kehuInvoice['company'],//销售方名称
				"stffName" => $staff['name'],//开票人
				"companyName" => $kehu['companyName'],//购买方
				"type" => $kehuInvoice['type'],//发票类型
				"money" => $kehuInvoice['money'],//价税合计
				"moneyInvoice" => $moneyInvoice,//开票金额
				"moneyTax" => $moneyTax,//纳税金额
				"text" => kong($kehuInvoice['text']),//开票说明
				"invoiceNum" => $kehuInvoice['invoiceNum'],//发票编号
				"open" => $kehuInvoice['open'],//是否开票
				"openDate" => $kehuInvoice['openDate'],//开票日期
				"ico" => root.$kehuInvoice['ico'],//发票扫描件
			);
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
}
?>